import 'package:flutter/material.dart';
import 'package:flutter_demo/page/home.dart';
import 'package:flutter_demo/page/detail.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // 定义路由
  final Map<String, Function> routes = {
    '/': (context) => HomePage(),
    '/detail': (context, {arguments}) => DetailPage(arguments: arguments),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
      onGenerateRoute: (RouteSettings settings) {
        // 路由切换统一处理
        final String name = settings.name;
        final Function pageBuilder = this.routes[name];

        if (settings.arguments != null) {
          
          // 设置路由参数
          final Route route = MaterialPageRoute(
            builder: (BuildContext context) => pageBuilder(context, arguments: settings.arguments),
          );
          return route;

        } else {
          
          // 不设置路由参数
          final Route route = MaterialPageRoute(
            builder: (context) => pageBuilder(context),
          );
          return route;

        }
      },
    );
  }
}
